package mju.cxf.config.listener;

import org.redisson.Redisson;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

//@Component
public class OrderListener {

    private final RBlockingQueue<Object> orderQueue;

//    @Autowired
    public OrderListener(RedissonClient redissonClient) {
        this.orderQueue = redissonClient.getBlockingQueue("orderQueue");
    }

    @PostConstruct
    public void startListening() {
        Thread listenerThread = new Thread(() -> {
            try {
                while (true) {
                    // 从队列中获取订单，阻塞直到有可用订单
                    Object order = orderQueue.take();
                    processOrder(order);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // 恢复中断状态
                System.out.println("订单监听线程被中断。");
            }
        });
        listenerThread.start();
    }

    private void processOrder(Object order) {
        // 处理订单的逻辑
        System.out.println("处理订单: " + order);
        // 这里可以添加更多的处理逻辑
    }
}
